//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "MarketModelVolProcess.h"
using namespace Cephei::QL::Models::Marketmodels::Evolvers;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::CMarketModelVolProcess (boost::shared_ptr<QuantLib::MarketModelVolProcess>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelVolProcess = NULL;
#endif
	_ppMarketModelVolProcess = &childNative;
    
}
Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::CMarketModelVolProcess (QuantLib::MarketModelVolProcess& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMarketModelVolProcess = NULL;
#endif
	_ppMarketModelVolProcess = new boost::shared_ptr<QuantLib::MarketModelVolProcess> (&childNative);
    
    _MarketModelVolProcessOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::CMarketModelVolProcess (CMarketModelVolProcess^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelVolProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMarketModelVolProcess = new boost::shared_ptr<QuantLib::MarketModelVolProcess> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::CMarketModelVolProcess (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelVolProcess = NULL;
#endif
	if (!t->IsSubclassOf(CMarketModelVolProcess::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::CMarketModelVolProcess (QuantLib::Handle<QuantLib::MarketModelVolProcess>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModelVolProcess = &childNative;
	_ppMarketModelVolProcess = &static_cast<boost::shared_ptr<QuantLib::MarketModelVolProcess>>(childNative.currentLink());
    
    _MarketModelVolProcessOwner = owner;
}
Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::CMarketModelVolProcess (QuantLib::Handle<QuantLib::MarketModelVolProcess> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModelVolProcess = &childNative;
	_ppMarketModelVolProcess = &static_cast<boost::shared_ptr<QuantLib::MarketModelVolProcess>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::CMarketModelVolProcess (QuantLib::MarketModelVolProcess childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelVolProcess = NULL;
#endif
	_ppMarketModelVolProcess = new boost::shared_ptr<QuantLib::MarketModelVolProcess> (new QuantLib::MarketModelVolProcess (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::~CMarketModelVolProcess ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMarketModelVolProcess != NULL)
    {
	    delete _ppMarketModelVolProcess;
        _ppMarketModelVolProcess = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::!CMarketModelVolProcess ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMarketModelVolProcess != NULL)
    {
	    delete _ppMarketModelVolProcess;
    }
}
QuantLib::MarketModelVolProcess& Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::GetReference ()
{
    if (_ppMarketModelVolProcess == NULL) throw gcnew NativeNullException ();
	return **_ppMarketModelVolProcess;
}
boost::shared_ptr<QuantLib::MarketModelVolProcess>& Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::GetShared ()
{
    if (_ppMarketModelVolProcess == NULL) throw gcnew NativeNullException ();
	return *_ppMarketModelVolProcess;
}
QuantLib::MarketModelVolProcess* Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::GetPointer ()
{
    if (_ppMarketModelVolProcess == NULL) throw gcnew NativeNullException ();
	return &**_ppMarketModelVolProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MarketModelVolProcess>& Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::GetHandle ()
{
	if (_phMarketModelVolProcess == NULL)
	{
		_phMarketModelVolProcess = new Handle<QuantLib::MarketModelVolProcess> (*_ppMarketModelVolProcess);
	}
	return *_phMarketModelVolProcess;
}
#endif
bool Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::HasNative () 
{
	return (_ppMarketModelVolProcess != NULL);
}

Cephei::QL::Models::Marketmodels::Evolvers::IMarketModelVolProcess^ Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::NextPath::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppMarketModelVolProcess)->nextPath ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::Nextstep (Cephei::IVector<Double>^ variates)
{
    CoVector<Double>^ _Cvariates;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cvariates = safe_cast<CoVector<Double>^> (variates);
        _Cvariates->Lock();
        INativeVector<Double>^ _NCIvariates = _Cvariates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCvariates = safe_cast<CDoubleVector^>(_NCIvariates);
        std::vector<QuantLib::Real>& _variates = static_cast<std::vector<QuantLib::Real>&> (_NCvariates->GetReference ());
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMarketModelVolProcess)->nextstep ( _variates );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cvariates != nullptr) _Cvariates->Unlock();
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::NumberStateVariables::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelVolProcess)->numberStateVariables ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::NumberSteps::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelVolProcess)->numberSteps ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::StateVariables::get ()
{
    try
    {
    	std::vector<QuantLib::Real>& _rv = (std::vector<QuantLib::Real>&)(*_ppMarketModelVolProcess)->stateVariables ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::StepSd::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMarketModelVolProcess)->stepSd ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Evolvers::CMarketModelVolProcess::VariatesPerStep::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelVolProcess)->variatesPerStep ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

